<template>
  <div class="role-container">
    <div v-auth-dir="`sys:role:list`" class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active:index === activeIndex}"
        @click="roleClickFn(index,item.roleId)"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <!-- <svg-icon icon-class="more" /> -->
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-auth-dir="`sys:role:add_edit`" @click.native="$router.push(`/add-role?roleId=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item v-auth-dir="`sys:role:remove`" @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button v-auth-dir="`sys:role:add_edit`" class="addBtn" size="mini" @click="$router.push('/add-role')">添加角色</el-button>
    </div>
    <!-- 右侧权限和成员 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <!-- 分为5列功能权限点(对应左侧菜单5个导航) -->
            <div v-for="(item) in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="myTree"
                :data="item.children"
                :props="{children: 'children',
                         label: 'title',
                         disabled:'disabled'}"
                show-checkbox
                default-expand-all
                check-strictly
                check-on-click-node
                :expand-on-click-node="false"
                node-key="id"
              ><template #default="{node}"><span style="color:#999">{{ node.label }}</span></template></el-tree>
              <!-- :default-checked-keys="roleDetailList[index]" -->
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${roleUserTotal})`" name="member">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs></div>
  </div>
</template>

<script>
import { delRoleUserAPI, getRoleDetailAPI, getRoleListAPI, getRoleUserAPI, getTreeListAPI } from '@/apis/system'

export default {
  name: 'Role',
  data() {
    return {
      roleList: [], // 现有角色列表
      activeIndex: 0, // 记录哪项该高亮
      treeList: [], // 所有权限点列表
      roleDetailList: [], // 用户已勾权限点
      activeName: 'tree', // 显示标签页名字
      roleUserList: [], // 指定角色的成员列表
      roleUserTotal: 0 // 当前角色下的成员总数
    } // default-checked-keys 空数组不能取消勾选
  },
  async created() {
    await this.getList()
    await this.getTreeList() // 获取所有功能权限点列表数据
    // 默认获取第一个角色的权限点列表
    // async 函数始终默认返回一个Promise对象-值来自于 async 函数里的 return 而这里没有return this.getList 原地的Promise
    // 对象的成功值是 undefined
    this.getRoleDetailListFn(this.roleList[0].roleId) // 开始获取第一个角色下属的权限点详情列表
    this.getMemberListFn(this.roleList[0].roleId) // 开始获取第一个角色下属的成员列表
  },
  methods: { // 获取角色列表
    async getList() {
      const res = await getRoleListAPI()
      // console.log(res)
      this.roleList = res.data
    }, // 点击某行角色
    roleClickFn(index, roleId) {
      this.activeIndex = index
      this.getRoleDetailListFn(roleId)
      this.getMemberListFn(roleId)
    }, // 请求所有功能权限列表
    async getTreeList() {
      const res = await getTreeListAPI()
      // console.log(res)
      // 目标：给所有对象都添加一个 flag 标记默认值
      // 因为每个对象到底有几层 children 不确定=> 递归遍历
      this.setdisabledFn(res.data)
      this.treeList = res.data
    }, // 设置属性禁用方法
    setdisabledFn(arr) {
      arr.forEach(obj => {
        obj.disabled = true
        if (obj.children) { this.setdisabledFn(obj.children) }
      })
    }, // 请求指定角色下属的功能权限列表
    async getRoleDetailListFn(roleId) {
      const roleRes = await getRoleDetailAPI(roleId)
      // console.log(roleRes)
      // 问题：tree组件 default-check-keys 赋予空数组不能取消勾选
      // 解决：tree组件的 setCheckKeys 可以传入空数组取消勾选
      this.roleDetailList = roleRes.data.perms
      // this.$refs.别名(如果别名有多个，值为数组集合)
      this.$refs.myTree.forEach((treeComponent, index) => {
        treeComponent.setCheckedKeys(this.roleDetailList[index])
      })
    }, // 请求指定角色下属的成员列表集合
    async getMemberListFn(roleId) {
      const res = await getRoleUserAPI(roleId)
      console.log(res)
      this.roleUserList = res.data.rows
      this.roleUserTotal = res.data.total
    }, // 删除角色
    delRole(roleId) {
      this.$confirm('是否确认删除当前角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleUserAPI(roleId)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.roleClickFn(0, this.roleList[0].roleId)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
